package com.epam.training;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.epam.training.domain.*;
import com.epam.training.service.bonus.BonusService;
import com.epam.training.service.car.CarService;
import com.epam.training.service.insurance.InsuranceService;
import com.epam.training.service.penalty.PenaltyService;
import com.epam.training.service.user.UserService;

@Controller
public class AdminPageController {
	// private static final Logger logger = LoggerFactory
	// .getLogger(AdminPageController.class);

	public static String ADMIN_PAGE = "admin";

	private final UserService userService;

	private final BonusService bonusService;

	private final PenaltyService penaltyService;

	private final CarService carService;

	private final InsuranceService insuranceService;

	private static final String PAGE_BEGIN_ATRIBUTE = "beginIndex";
	private static final String PAGE_END_ATRIBUTE = "endIndex";
	private static final String PAGE_CURRENT_ATRIBUTE = "currentIndex";

	private static final String ADMINLIST_ATRIBUTE = "adminList";
	private static final String CUSTOMERLIST_ATRIBUTE = "customerList";
	private static final String BONUSLIST_ATRIBUTE = "bonusList";
	private static final String PENALTYLIST_ATRIBUTE = "penaltyList";
	private static final String INSURANCELIST_ATRIBUTE = "insuranceList";
	private static final String CARLIST_ATRIBUTE = "carList";
	private static final String ADD_USER_ATRIBUTE = "addUser";

	@Autowired
	public AdminPageController(UserService userService,
			BonusService bonusService, PenaltyService penaltyService,
			CarService carService, InsuranceService insuranceService) {
		this.userService = userService;
		this.bonusService = bonusService;
		this.penaltyService = penaltyService;
		this.carService = carService;
		this.insuranceService = insuranceService;
	}

	@RequestMapping(value = "/admin/show/admins", method = RequestMethod.GET)
	private String showAllAdmins(Model model) {
		return "redirect:/admin/show/admins/pages/1";
	}

	@RequestMapping(value = "/admin/show/admins/pages/{pageNumber}", method = RequestMethod.GET)
	private String showAllAdminsPages(@PathVariable Integer pageNumber,
			Model model) {
		Page<Admin> page = userService.getAllAdmins(pageNumber);

		model.addAttribute(ADMINLIST_ATRIBUTE, page);
		setPageableAtribute(page, model);

		return "adminView/AdminList";
	}

	@RequestMapping(value = "/admin/show/customers", method = RequestMethod.GET)
	private String showAllCustomers(Model model) {
		return "redirect:/admin/show/customers/pages/1";
	}

	@RequestMapping(value = "/admin/show/customers/pages/{pageNumber}", method = RequestMethod.GET)
	private String showAllCustomersByPage(@PathVariable Integer pageNumber,
			Model model) {
		Page<Customer> page = userService.getAllCustomers(pageNumber);

		model.addAttribute(CUSTOMERLIST_ATRIBUTE, page);
		setPageableAtribute(page, model);

		return "adminView/CustomerList";
	}

	@RequestMapping(value = "/admin/show/bonus", method = RequestMethod.GET)
	private String showAllBonus(Model model) {
		return "redirect:/admin/show/bonus/pages/1";
	}

	@RequestMapping(value = "/admin/show/bonus/pages/{pageNumber}", method = RequestMethod.GET)
	private String showAllBonusOnPage(@PathVariable Integer pageNumber,
			Model model) {
		Page<Bonus> page = bonusService.getAllBonus(pageNumber);

		model.addAttribute(BONUSLIST_ATRIBUTE, page);
		setPageableAtribute(page, model);

		return "adminView/BonusList";
	}

	@RequestMapping(value = "/admin/show/penalty", method = RequestMethod.GET)
	private String showAllPenalty(Model model) {
		return "redirect:/admin/show/penalty/pages/1";
	}

	@RequestMapping(value = "/admin/show/penalty/pages/{pageNumber}", method = RequestMethod.GET)
	private String showAllPenaltyOnPage(@PathVariable Integer pageNumber,
			Model model) {
		Page<Penalty> page = penaltyService.getAllPenalty(pageNumber);

		model.addAttribute(PENALTYLIST_ATRIBUTE, page);
		setPageableAtribute(page, model);

		return "adminView/PenaltyList";
	}

	@RequestMapping(value = "/admin/show/car", method = RequestMethod.GET)
	private String showAllCar(Model model) {
		return "redirect:/admin/show/car/pages/1";
	}

	@RequestMapping(value = "/admin/show/car/pages/{pageNumber}", method = RequestMethod.GET)
	private String showAllCar(@PathVariable Integer pageNumber, Model model) {
		Page<Car> page = carService.getAllCar(pageNumber);

		model.addAttribute(CARLIST_ATRIBUTE, page);
		setPageableAtribute(page, model);

		return "adminView/CarList";
	}

	private <T> void setPageableAtribute(Page<T> page, Model model) {
		int current = page.getNumber() + 1;
		int begin = Math.max(1, current - 5);
		int end = Math.min(begin + 10, page.getTotalPages());

		model.addAttribute(PAGE_BEGIN_ATRIBUTE, begin);
		model.addAttribute(PAGE_END_ATRIBUTE, end);
		model.addAttribute(PAGE_CURRENT_ATRIBUTE, current);
	}

	@RequestMapping(value = "/admin/show/insurance", method = RequestMethod.GET)
	private String showAllInsurance(Model model) {
		return "redirect:/admin/show/insurance/pages/1";
	}

	@RequestMapping(value = "/admin/show/insurance/pages/{pageNumber}", method = RequestMethod.GET)
	private String showAllInsurance(@PathVariable Integer pageNumber,
			Model model) {
		Page<Insurance> page = insuranceService.getAllInsurance(pageNumber);

		model.addAttribute(INSURANCELIST_ATRIBUTE, page);
		setPageableAtribute(page, model);

		return "adminView/InsuranceList";
	}

	@RequestMapping(value = "/admin/add/user", method = RequestMethod.GET)
	private String addUserForm(Model model) {
		boolean addUser = true;
		model.addAttribute(ADD_USER_ATRIBUTE, addUser);

		return "adminView/AddUser";
	}

	@RequestMapping(value = "/admin/add/bonus", method = RequestMethod.GET)
	private String addBonusForm(Model model) {
		boolean addBonus = true;
		model.addAttribute("addBonus", addBonus);

		return "adminView/AddBonus";
	}

	@RequestMapping(value = "/admin/add/car", method = RequestMethod.GET)
	private String addCarForm(Model model) {
		boolean addCar = true;
		model.addAttribute("addCar", addCar);

		return "adminView/AddCar";
	}

	@RequestMapping(value = "/admin/add/insurance", method = RequestMethod.GET)
	private String addInsuranceForm(Model model) {
		boolean addInsurance = true;
		model.addAttribute("addInsurance", addInsurance);

		return "adminView/AddInsurance";
	}

	@RequestMapping(value = "/admin/add/penalty", method = RequestMethod.GET)
	private String addPenaltyForm(Model model) {
		boolean addPenalty = true;
		model.addAttribute("addPenalty", addPenalty);

		return "adminView/AddPenalty";
	}

	@RequestMapping(value = "/admin/update/admin", method = RequestMethod.POST)
	private String updateAdmin(@RequestParam Integer adminId, Model model) {
		Admin admin = userService.getAdminById(adminId);
		model.addAttribute("admin", admin);

		return "adminView/UpdateAdmin";
	}

	@RequestMapping(value = "/admin/update/customer", method = RequestMethod.POST)
	private String updateCustomer(@RequestParam Integer customerId, Model model) {
		Customer customer = userService.getCustomerById(customerId);
		model.addAttribute("customer", customer);

		return "adminView/UpdateCustomer";
	}

	@RequestMapping(value = "/admin/update/car", method = RequestMethod.POST)
	private String updateCar(@RequestParam Integer carId, Model model) {
		Car car = carService.getById(carId);
		model.addAttribute("car", car);

		return "adminView/UpdateCar";
	}

	@RequestMapping(value = "/admin/update/insurance", method = RequestMethod.POST)
	private String updateInsurance(@RequestParam Integer insuranceId,
			Model model) {
		Insurance insurance = insuranceService.getById(insuranceId);
		model.addAttribute("insurance", insurance);

		return "adminView/UpdateInsurance";
	}

	@RequestMapping(value = "/admin/update/penalty", method = RequestMethod.POST)
	private String updatePenalty(@RequestParam Integer penaltyId, Model model) {
		Penalty penalty = penaltyService.getById(penaltyId);
		model.addAttribute("penalty", penalty);

		return "adminView/UpdatePenalty";
	}

	@RequestMapping(value = "/admin/update/bonus", method = RequestMethod.POST)
	private String updateBonus(@RequestParam Integer bonusId, Model model) {
		Bonus bonus = bonusService.getById(bonusId);
		model.addAttribute("bonus", bonus);

		return "adminView/UpdateBonus";
	}

	@RequestMapping(value = "/admin/delete/admin", method = RequestMethod.POST)
	private String deleteAdmin(@RequestParam Integer adminId, Model model) {
		userService.deleteAdmin(adminId);
		model.addAttribute("message", "admin.delete");

		return "/admin";
	}

	@RequestMapping(value = "/admin/delete/customer", method = RequestMethod.POST)
	private String deleteCustomer(@RequestParam Integer customerId, Model model) {
		userService.deleteCustomer(customerId);
		model.addAttribute("message", "customer.delete");

		return "/admin";
	}

	@RequestMapping(value = "/admin/delete/car", method = RequestMethod.POST)
	private String deleteInsurance(@RequestParam Integer carId, Model model) {
		carService.delete(carId);
		model.addAttribute("message", "car.delete");

		return "/admin";
	}
}
